package com.olap.starter.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.olap.starter.common.service.cache.TableCacheService;
import com.olap.starter.common.service.cache.TableConfigCaCheService;
import com.olap.starter.service.TableConfigColumnServices;
import com.olap.starter.service.TableConfigServices;
import com.olap.starter.core.base.BaseController;
import com.olap.starter.core.response.Result;
import com.olap.starter.pojo.TableConfig;
import com.olap.starter.pojo.TableConfigColumn;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author WBG
 * @since 2022-08-31
 */
@RestController
@RequestMapping("/online/tableConfigColumn")
public class TableConfigColumnController extends BaseController<TableConfigColumn, TableConfigColumnServices> {

    @Autowired
    private TableConfigServices tableConfigServices;
    @Autowired
    private TableConfigCaCheService tableConfigCaCheService;
    @Autowired
    private TableCacheService tableCacheService;

    /**
     * 根据父id获取表字段配置
     * @param id
     * @return
     */
    @GetMapping({"/queryByParentId"})
    public Result<?> queryByParentId(@RequestParam(name = "id", required = true) String id) {
        LambdaQueryWrapper<TableConfigColumn> wrap = Wrappers.lambdaQuery(TableConfigColumn.class)
                .eq(TableConfigColumn::getParentId, Long.parseLong(id))
                .eq(TableConfigColumn::getColumnAuth, StringPool.ONE)
                .orderByAsc(TableConfigColumn::getSort);
        List<TableConfigColumn> tableConfigColumns = this.getService().getBaseMapper().selectList(wrap);
        return success(tableConfigColumns);
    }

    /**
     * 根据父id获取分组表头表字段配置
     * @param id
     * @return
     */
    @GetMapping({"/queryGroupByParentId"})
    public Result<?> queryGroupByParentId(@RequestParam(name = "id", required = true) String id) {
        List<TableConfigColumn> tableConfigColumns = tableConfigServices.queryGroupByParentId(id);
        return success(tableConfigColumns);
    }

    /**
     * 批量编辑
     * @param configColumnList
     * @return
     */
    @PutMapping({"/editBatch"})
    public Result<?> editBatch(@RequestBody List<TableConfigColumn> configColumnList) {
        this.getService().updateBatchById(configColumnList);
        TableConfig config = tableConfigCaCheService.reloadCache(configColumnList.get(0).getUuid(), configColumnList.get(0).getTheme());
        tableCacheService.reloadCache(config);
        return success("编辑成功！");
    }

}
